*********************************************************************
********Preparation of ITANES 1996 for independence******************
*********************************************************************

* directory definitions 

	project, doinfo
	global pdir "`r(pdir)'"							// the project's main dir.
	global dofile "`r(dofile)'"						// do-file's stub name
	global data_original = "$pdir/data_original"  //data directory for coded data 
	global data_coded = "$pdir/data_coded"  //data directory for coded data 
	global figures = "$pdir/results/figures"  //data directory for figures
	global tables = "$pdir/results/tables"  //data directory for tables

*
	// 1996 election study for placebo with IV: 
	project, original("$data_original/election studies/1996/ENG_1996.dta")

	use "$data_original/election studies/1996/ENG_1996.dta", clear
	rename q236 comune_id 
	rename q235 province_id
	
	tempfile itanes1996
	save `itanes1996'

*
	// read in ugs data:
	project, uses("$data_coded/placebased_mapdata.dta")
	
	use "$data_coded/placebased_mapdata.dta", clear 
	keep comune_id ugs_yn km_to_ugs
	
	tempfile ugs
	save `ugs'

*
	// we also need pre-treatment population. Here we can simply use the same Campante data as the ugs stations:
	project, original("$data_original/Campanteetal/mergefile.xls")
	
	import excel "$data_original/Campanteetal/mergefile.xls", sheet("COMUNI 11_03_2013") firstrow clear 

	** rename: 
	rename E comune_id
	rename Popolazionelegale200121102 population_2001

	** minimalize data: 
	keep comune_id population_2001

	tempfile pop2001
	save `pop2001'

*
	// Important our webscraped M5S data and make some cleanings:
	project, original("$data_coded/events_comune_neigh_nga.csv")

	import delimited "$data_coded/events_comune_neigh_nga.csv", clear 

	** date cleanings:
	replace hist_days=round(hist_days, 0.1)
	replace hist_days=0 if hist_days==.
	
	keep wn_* comune_id hist_days
	
	** replace missings (if any)
	foreach w of varlist wn_* {
		replace `w'=0 if `w'==.
	}
	
	** minimalize data: 
	keep comune_id wn_treat_campaign hist_days

	tempfile meetup
	save `meetup'

*
	// open survey data and merge:
	use `itanes1996'

	merge m:m comune_id using `ugs'
	drop if _merge==2
	drop _merge
	
	merge m:m comune_id using `pop2001'
	drop if _merge==2
	drop _merge
	
	merge m:m comune_id using `meetup'
	drop if _merge==2
	drop _merge

	** we standardize the exposure variable as discussed in the paper 
	gen exposure_pop=population_2001/1000

	foreach var of varlist wn_* {
		gen std_`var'=log((`var'/log(exposure_pop))+1)
	}
	
	** create constant to trick reghdfe
	gen constant=1

	*clean the data & make it meaningful based on codebook:

	gen urbanity=q238
	
	gen pol_int=q5
	revrs pol_int, replace
	
	gen radio=q33
	
	foreach var of varlist q39-q41 {
		gen efficacy_`var'=`var'
		revrs efficacy_`var', replace
	}
	rename efficacy_q39 efficacy_self 
	rename efficacy_q40 efficacy_rep 
	rename efficacy_q41 efficacy_cand
	
	gen democrat=0
	replace democrat=1 if q42==1
	
	gen dem_sat=q43
	revrs dem_sat 
	
	gen lr=q44
	
	gen scal_berlu=q107
	gen scal_bossi=q109
	gen scal_fini=q110
	gen scal_scalfaro=q110
	
	gen party_id=q131
	tab party_id, gen(party_id)
	
	gen vote94=q133
	tab vote94, gen(vote94_)
	
	gen turnout96=0
	replace turnout96=1 if q143==1
	
	gen vote96pro=q154
	tab vote96pro, gen(vote96pro_)
	
	gen vote96maj=q151
	tab vote96maj, gen(vote96maj_)
	
	gen age=q198+17
	
	gen unemployed=0
	replace unemployed=1 if q202==5
	
	gen female=0
	replace female=1 if q234==2
	
	gen econ_worse=0 if  q3!=.
	replace econ_worse=1 if  q3==2
	
	gen econ_worse_personal=0 if  q4!=.
	replace econ_worse_personal=1 if  q4==2
	
	gen education=q199
	
	gen religiosity=q136
	revrs religiosity, replace
	
	gen log_hist=log(hist_days+1)
	gen log_ugs=log(km_to_ugs+1)
	
	**renaming exposure variables to ease up reading of analyses code:
	*rename std_n_treat_referendum m5s_referendum
	*rename n_total m5s_total
	
	**labeling variables 
	lab var hist_days "days since formation"
	lab var log_hist "log days since formation"
	lab var log_ugs "log distance to closest UGS (in km)"
	lab var religiosity "church attendance (1-5)"	
	lab var age "age (18-85)"	
	lab var unemployed "unemployed (0,1)"	
	lab var education "education (1-7)"	
	lab var female "female (0,1)"	
	lab var pol_int "political interest (1-4)"
	lab var radio "radio (1-5)"
	lab var efficacy_self "self efficacy (1-4)"
	lab var efficacy_rep "candidates lose touch (1-4)"
	lab var efficacy_cand "candidates not parties relevant (1-4)"
	lab var democrat "supports democracy (0,1)"
	lab var dem_sat "satisfied with demo in Italy (1-4)"
	lab var lr "left-right self (1-5)"
	lab var scal_berlu "scalometer Berlusconi (1-10)"
	lab var scal_bossi "scalometer Bossi (1-10)"
	lab var scal_scalfaro "scalometer Scalfaro (1-10)"
	lab var party_id6 "PiD PD (0,1)"
	lab var party_id11 "PiD AN (0,1)"
	lab var party_id8 "PiD FI (0,1)"
	lab var party_id7 "PiD RC (0,1)"
	lab var party_id12 "PiD LN (0,1)"
	lab var party_id1 "PiD Populari (0,1)"
	lab var vote94_3 "PD 1994 (0,1)"
	lab var vote94_6 "FI 1994 (0,1)"
	lab var vote94_7 "AN 1994 (0,1)"
	lab var vote94_8 "LN 1994 (0,1)"
	lab var vote94_1 "PPI 1994 (0,1)"
	lab var vote94_4 "RC 1994 (0,1)"
	lab var turnout96 "turnout 1996 (0,1)"
	lab var vote96pro_3 "PD 1996 (0,1)"
	lab var vote96pro_5 "FI 1996 (0,1)"
	lab var vote96pro_7 "AN 1996 (0,1)"
	lab var vote96pro_8 "LN 1996 (0,1)"
	lab var vote96pro_1 "PPI 1996 (0,1)"
	lab var vote96pro_4 "RC 1996 (0,1)"
	lab var vote96maj_1 "Ulivo (0,1)"
	lab var vote96maj_2 "POL (0,1)"
	lab var vote96maj_3 "LN (0,1)"
	lab var std_wn_treat_campaign "M5S: referendum"
	lab var population_2001 "population in 2001"
	
	save "$data_coded/placebased_independence.dta", replace

* 
	// report any data we create with this do file: 
	project, creates("$data_coded/placebased_independence.dta")	

